//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tmf_core\main.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE("Start");

private ["_side_players","_side_unit","_sides","_confmapsize"];

if (isServer) then {
	GVAR(mission_event_handler) = [];
	publicVariable QGVAR(mission_event_handler);
};
QGVAR(mission_event_handler) addPublicVariableEventHandler {
	// We use the value of the variable as argument to the XNetRunEvent function.
	// _this contains ["VarName", VarValue]
	(_this select 1) call FFUNC(runevent);
};

// For safety
GVAR(SERVER) = isServer;
GVAR(CLIENT) = !isDedicated;
GVAR(HCCLIENT) = !isDedicated && !hasInterface;

// Events common to both server and client
[0,QGVAR(all_object), {_this spawn FFUNC(registerObject);}] call FFUNC(addevent);
// Events common to both server and client
[0,QGVAR(all_var), {_this spawn FFUNC(setMissionVar);}] call FFUNC(addevent);
// create/update/delete a mission marker on all systems
[0,QGVAR(all_marker), {_this spawn FFUNC(updateMarker);}] call FFUNC(addevent);
// create/delete a moving marker on all systems
[0,QGVAR(all_object_marker), {_this spawn FFUNC(updateObjectMarker);}] call FFUNC(addevent);
// Executes an action triggered by a mission object on all systems
[0,QGVAR(all_action), {_this call FFUNC(executeObjectAction);}] call FFUNC(addevent);
// Creates/Updates a mission task on all systems
[0,QGVAR(all_task), {_this spawn FFUNC(updateTask);}] call FFUNC(addevent);
// Cleans all mission elements
[0,QGVAR(all_clean), {_this call FFUNC(cleanEvent);}] call FFUNC(addevent);
// Deletes ruins left by destroyed objects (radio tower, etc.)
[0,QGVAR(all_delruin), {_ruins = _this nearObjects ["Ruins",30];if (count _ruins > 0) then {{deleteVehicle _x}foreach _ruins;}}] call FFUNC(addevent);
// Launches the mission with the ID and parameters given
[0,QGVAR(all_start), {_this call FFUNC(launchMission);}] call FFUNC(addevent);
// Launches the mission with the ID and parameters given
[0,QGVAR(all_stop), {_this call FFUNC(stopMission);}] call FFUNC(addevent);
// Executes a mission function on all systems.
[0,QGVAR(all_execute), {_fnct = _this select 0; _params = _this select 1; _fnc = missionNameSpace getVariable [_fnct,{}]; _params spawn _fnc;}] call FFUNC(addevent);
// Executes a mission function on all systems.
[0,QGVAR(all_message), {_this call FFUNC(handleMessage)}] call FFUNC(addevent);

["CURRENT_WORLD",GVAR(current_world)] call FFUNC(setFrameworkValue);
_confmapsize = getNumber(configFile/"CfgWorlds"/worldName/"mapSize");
["WORLD_CENTER",[_confmapsize / 2,_confmapsize / 2,0]] call FFUNC(setFrameworkValue);

["USE_EXTERNAL_MISSIONS",tmf_use_external_missions] call FFUNC(setFrameworkValue);
["BASE_CONQUERED_TIMEOUT",tmf_conquered_base_timeout] call FFUNC(setFrameworkValue);

["MISSION_FRAMEWORK_PATH",QUOTE(TMF_PATH) + "\"] call FFUNC(setFrameworkValue);
["MISSION_HEADER_FORMAT","m"] call FFUNC(setFrameworkValue);
["MISSION_CHAPTER_FORMAT","c"] call FFUNC(setFrameworkValue);
["STATIC_MISSIONS_COUNT",0] call FFUNC(setFrameworkValue);
["STATIC_MISSIONS_PATH",QPATH(missions\)] call FFUNC(setFrameworkValue);
["EXTERNAL_MISSIONS_COUNT",0] call FFUNC(setFrameworkValue);
["EXTERNAL_MISSIONS_PATH","\tmfmissions\"] call FFUNC(setFrameworkValue);

["IA_SKILL",[0.65,0.2]] call FFUNC(setFrameworkValue);

["DEFAULT_EXPLOSIVES",["SatchelCharge_Remote_Ammo","DemoCharge_Remote_Ammo"]] call FFUNC(setFrameworkValue);
["DLG_WEATHER",[[localize "STR_KW_RANDOM",-1,-1],[localize "STR_KW_CLEAR",0,0.2],[localize "STR_KW_CLOUDY",0,0.8],[localize "STR_KW_LIGHTRAIN",0.3,0.7],[localize "STR_KW_MEDIUMRAIN",0.5,0.7],[localize "STR_KW_STORMY",0.8,1]]] call FFUNC(setFrameworkValue);
["DLG_POWER",[[localize "STR_KW_RANDOM",-1,-1],[localize "STR_KW_AUTOMATIC",0,0],[localize "STR_KW_IMPROVED",1,0],[localize "STR_KW_LEVELEDUP",2,0],[localize "STR_KW_HEAVY",3,1],[localize "STR_KW_NIGHTMARE",4,3]]] call FFUNC(setFrameworkValue);
["DLG_AIRSUPPORT",[[localize "STR_KW_RANDOM",-1],[localize "STR_KW_NONE",0],[localize "STR_KW_FEW",0.3],[localize "STR_KW_MEDIUM",0.6],[localize "STR_KW_NORMAL",1],[localize "STR_KW_INCREASED",1.6],[localize "STR_KW_HEAVY",2],[localize "STR_KW_UNBEARABLE",4]]] call FFUNC(setFrameworkValue);
["DLG_REINFORCEMENT",[[localize "STR_KW_RANDOM",-1],[localize "STR_KW_NONE",0],[localize "STR_KW_RARE",1],[localize "STR_KW_REGULAR",2],[localize "STR_KW_FREQUENT",5],[localize "STR_KW_TOAST",7]]] call FFUNC(setFrameworkValue);
["DLG_COMMUNICATION",[[localize "STR_KW_RANDOM",-1],["100m",100],["200m",200],["300m",300],["400m",400],["500m",500],["600m",600],["700m",700],["800m",800],["900m",900]]] call FFUNC(setFrameworkValue);
["DLG_SNIPERS",[[localize "STR_KW_RANDOM",-1,-1],[localize "STR_KW_NONE",0,0],[localize "STR_KW_RARE",0,1],[localize "STR_KW_FEW",1,1],[localize "STR_KW_SOME",1,3],[localize "STR_KW_ALOT",2,4],[localize "STR_KW_EVERYWHERE",5,8]]] call FFUNC(setFrameworkValue);
["DLG_CIVILIANS",[[localize "STR_KW_NO",0,0],[localize "STR_KW_YES",1,1]]] call FFUNC(setFrameworkValue);
["KRON_UPS_SHAREDIST",(If (isNil "KRON_UPS_sharedist") then {300} else {KRON_UPS_sharedist})] call FFUNC(setFrameworkValue);

["REINF_AIRBORNE_RANGE",[3500,4500]] call FFUNC(setFrameworkValue);
["REINF_MECHANISED_RANGE",[1500,2000]] call FFUNC(setFrameworkValue);
["REINF_INFANTRY_RANGE",[1000,1300]] call FFUNC(setFrameworkValue);

["VEHICLE_CREW_DEFAULT_WEST","B_crew_F"] call FFUNC(setFrameworkValue);
["VEHICLE_CREW_DEFAULT_EAST","O_crew_F"] call FFUNC(setFrameworkValue);
["VEHICLE_CREW_DEFAULT_RESISTANCE","I_crew_F"] call FFUNC(setFrameworkValue);
["VEHICLE_CREW_DEFAULT_CIVILIAN","C_man_1_1_F"] call FFUNC(setFrameworkValue);

["VEHICLE_PILOT_DEFAULT_WEST","B_Helipilot_F"] call FFUNC(setFrameworkValue);
["VEHICLE_PILOT_DEFAULT_EAST","0_Helipilot_F"] call FFUNC(setFrameworkValue);
["VEHICLE_PILOT_DEFAULT_RESISTANCE","C_man_1_1_F"] call FFUNC(setFrameworkValue);
["VEHICLE_PILOT_DEFAULT_CIVILIAN","C_man_1_1_F"] call FFUNC(setFrameworkValue);

["AIR_TRANSPORT_WEST","B_Heli_Transport_01_F"] call FFUNC(setFrameworkValue);
["AIR_TRANSPORT_EAST","O_Heli_Light_02_unarmed_F"] call FFUNC(setFrameworkValue);
["AIR_TRANSPORT_RESISTANCE","I_Heli_Transport_02_F"] call FFUNC(setFrameworkValue);
["AIR_TRANSPORT_CIVILIAN","I_Heli_Transport_02_F"] call FFUNC(setFrameworkValue);

["LAND_TRANSPORT_WEST","B_Truck_01_covered_F"] call FFUNC(setFrameworkValue);
["LAND_TRANSPORT_EAST","O_Truck_02_covered_F"] call FFUNC(setFrameworkValue);
["LAND_TRANSPORT_RESISTANCE","I_Truck_02_covered_F"] call FFUNC(setFrameworkValue);
["LAND_TRANSPORT_CIVILIAN","C_Van_01_transport_F"] call FFUNC(setFrameworkValue);

["INFANTRY_GROUP_WEST",["B_Soldier_TL_F","B_Soldier_GL_F","B_soldier_AR_F","B_soldier_LAT_F","B_Soldier_F","B_medic_F"]] call FFUNC(setFrameworkValue);
["INFANTRY_GROUP_EAST",["O_Soldier_TL_F","O_Soldier_GL_F","O_soldier_AR_F","O_soldier_LAT_F","O_Soldier_F","O_medic_F"]] call FFUNC(setFrameworkValue);
["INFANTRY_GROUP_RESISTANCE",["I_Soldier_TL_F","I_Soldier_GL_F","I_soldier_AR_F","I_soldier_LAT_F","I_Soldier_F","I_medic_F"]] call FFUNC(setFrameworkValue);
["INFANTRY_GROUP_CIVILIAN",["C_man_w_worker_F","C_man_p_fugitive_F_euro","C_man_hunter_1_F","C_man_shorts_1_F_euro","C_man_polo_1_F_euro"]] call FFUNC(setFrameworkValue);

["TYPE_LANDMINE","ATMine"] call FFUNC(setFrameworkValue);

["EMPTY_ARRAY",[]] call FFUNC(setFrameworkValue);
	
["MP_RIGHTS_CHECK",false] call FFUNC(setFrameworkValue);

[] call FFUNC(initCommon);

if (GVAR(SERVER)) then {
	// Stores all players related information
	GVAR(player_store) = "Land_HelipadEmpty_F" createVehicleLocal [0, 0, 0];
	// Stores all objects placed by the mission framework
	GVAR(placed_objs_store) = "Land_HelipadEmpty_F" createVehicleLocal [0, 0, 0];

	[1,QGVAR(s_execute), FFUNC(execS)] call FFUNC(addevent);
	[1,QGVAR(s_log), {diag_log format _this}] call FFUNC(addevent);
	[1,QGVAR(jip_var), {_this spawn FFUNC(setMissionJIPVar);}] call FFUNC(addevent);
	[1,QGVAR(s_store_vehicle),{_this spawn FFUNC(storeVehicle)}] call FFUNC(addevent);
	
	[] call FFUNC(initServer);
	
	[] spawn FFUNC(loadMissions);
};

if (!isDedicated && !hasInterface) then {
	[5,QGVAR(hc_spawn), { if (idxHC == (_this select 1)) then { (_this select 0) spawn EFUNC(spawn);};}] call FFUNC(addevent);
	[5,QGVAR(hc_execute), { if (idxHC == (_this select 2)) then { [(_this select 0),(_this select 1)] call FFUNC(execHC);};}] call FFUNC(addevent);
};

if (GVAR(CLIENT)) then {
	[2,QGVAR(c_execute), FFUNC(execC)] call FFUNC(addevent);
	
	if !(isNil "ioc_fnc_handlerAdd") then {
		_unique_id = ["MSO","EnterVehicle",QUOTE(FFUNC(vehicleEnter))] call ioc_fnc_handlerAdd;
		_unique_id = ["MSO","ExitVehicle",QUOTE(FFUNC(vehicleExit))] call ioc_fnc_handlerAdd;
	};

	player addEventHandler ["killed",{_this call FFUNC(initClient)}];
	[player,objNull] call FFUNC(initClient);
};
TRACE("End");